using UnityEngine;
using System.Collections;

/// \class  gkSpawningRandomAbscisseInGame
/// \brief  Select randomly an abscisse that is in the game volume
[AddComponentMenu("GK/Engine/Game/Spawn/gkSpawningRandomAbscisseInGame")]
public class gkSpawningRandomAbscisseInGame : gkSpawningModifier
{
	/// The minimum drop abscisse in screen percent
	public float DropAbscissePercentMin = 0.0f;
	
	/// The maximum drop abscisse in screen percent
	public float DropAbscissePercentMax = 1.0f;
	
	/// The game volume bounding box
	private Bounds m_oGameVolumeBoudingBox;
	
	/// \brief  Called just after the game begin to play
    void Start()
    {
	   // Get the spawning volume bounding box
	   gkUtilityBounds.GrabColliderOrRendererBounds(GameObject.Find("GameVolume"), out m_oGameVolumeBoudingBox);
    }
	
	/// \brief  Event called at the and of the actor spawn
	public override void ModifySpawnedActor(GameObject a_rSpawnedActor, int a_iNumActorsInCurrentBatch, int a_iNbActorsInCurrentBatch)
	{
		RandomAbscisse(a_rSpawnedActor);
	}
	
	/// \brief  Select randomly an abscisse that is in the game volume
	void RandomAbscisse(GameObject a_rSpawnedActor)
	{
		Vector3 f3SpawnedPosition;
		float fVolumeWidth;
		float fActorHalfWidth;
		float fMinPositionX;
		float fMaxPositionX;
		Bounds rSpawnedActorBounds;
		
		// Grab the spawned actor bounds
		 gkUtilityBounds.GrabRendererOrColliderBounds(a_rSpawnedActor, out rSpawnedActorBounds);
	
		// Get the current position
		f3SpawnedPosition = a_rSpawnedActor.transform.position;
		
		// Compute the min and max position
		fVolumeWidth = m_oGameVolumeBoudingBox.max.x - m_oGameVolumeBoudingBox.min.x;
		fActorHalfWidth = rSpawnedActorBounds.extents.x;
		fMinPositionX = m_oGameVolumeBoudingBox.min.x + fVolumeWidth * DropAbscissePercentMin + fActorHalfWidth;
		fMaxPositionX = m_oGameVolumeBoudingBox.min.x + fVolumeWidth * DropAbscissePercentMax - fActorHalfWidth;
	
		// Choose a random abscisse to drop the object between the min and max position
		f3SpawnedPosition.x = Random.Range(fMinPositionX, fMaxPositionX);
	
		// Set the new position
		a_rSpawnedActor.transform.position = f3SpawnedPosition;
	}
}
